﻿
using System.Collections.Generic;
using System.Data;
using System;
using ZHDJ.MobileInterface.Common;

namespace ZHDJ.MobileInterface
{
    public partial class ToXHPartyTransmission : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            ToXHPartyTransmissionOutParam result = new ToXHPartyTransmissionOutParam();
            ToXHPartyTransmissionInParam param = new ToXHPartyTransmissionInParam();
            string logMsg = "";
            bool isFlag = true;
            string hdID = "";
            string NewID = StringHelper.NewID;

            try
            {
                param = inParam as ToXHPartyTransmissionInParam;

                if (string.IsNullOrEmpty(param.BranchID))
                {
                    result.code = "0";
                    result.desc = "组织ID不能为空";

                    isFlag = false;
                }


                if (isFlag)
                {
                    if (string.IsNullOrEmpty(param.Content) || string.IsNullOrEmpty(param.Age) || string.IsNullOrEmpty(param.Title) ||
                        string.IsNullOrEmpty(param.Gender) || string.IsNullOrEmpty(param.Post) || string.IsNullOrEmpty(param.PoliticsLineament) ||
                        string.IsNullOrEmpty(param.Address) || string.IsNullOrEmpty(param.Zip) || string.IsNullOrEmpty(param.PartyDeputys))
                    {
                        result.code = "0";
                        result.desc = "年龄、性别、职务、政治面貌、工作单位、联系地址、邮政编号、党代表ID、主题、反映内容不能为空";

                        isFlag = false;
                    }
                }

                int age = 0;
                int zip = 0;

                if (isFlag)
                {
                    try { age = Convert.ToInt32(param.Age); }
                    catch
                    {
                        result.code = "0";
                        result.desc = "年龄必须为整型数据";

                        isFlag = false;
                    }
                }
                if (isFlag)
                {
                    try { age = Convert.ToInt32(param.Zip); }
                    catch
                    {
                        result.code = "0";
                        result.desc = "邮编必须为整型数据";

                        isFlag = false;
                    }
                }

                if (isFlag)
                {
                    if (string.IsNullOrEmpty(param.LoginID))
                    {
                        if (string.IsNullOrEmpty(param.UserName) || string.IsNullOrEmpty(param.MobileNo))
                        {
                            result.code = "0";
                            result.desc = "姓名,联系方式不能为空";
                        }
                    }
                    else
                    {
                        DataTable dt = XHPartyTransmission.GetDeputyName(param.LoginID);
                        if (dt.Rows.Count > 0)
                        {
                            if (string.IsNullOrEmpty(param.UserName))
                            {
                                //获取姓名
                                param.UserName = dt.Rows[0]["UserName"].ToString();
                            }
                            if (string.IsNullOrEmpty(param.MobileNo))
                            {
                                //获取电话
                                param.MobileNo = dt.Rows[0]["MobileNo"].ToString();
                            }
                        }
                    }
                }

                if (isFlag)
                {
                    DataTable dt = XHPartyTransmission.GetDeputyName(param.PartyDeputys);
                    if (dt.Rows.Count > 0)
                    { param.PartyDeputyName = dt.Rows[0]["UserName"].ToString(); }
                    else
                    {
                        param.PartyDeputyName = "";
                    }
                    ZHDJ.MobileInterface.Common.XHPartyTransmission.FieldAttribute FA = new Common.XHPartyTransmission.FieldAttribute()
                    {
                        ID = NewID,                        
                        BranchID = param.BranchID,
                        UserName = param.UserName,
                        MobileNo = param.MobileNo,
                        Age = Convert.ToInt32(param.Age),
                        Gender = param.Gender,
                        Post = param.Post,
                        PoliticsLineament = param.PoliticsLineament,
                        WorkUnit = param.WorkUnit,
                        Address = param.Address,
                        Zip = param.Zip,
                        PartyDeputys = param.PartyDeputys,
                        PartyDeputyName = param.PartyDeputyName,
                        Title = param.Title,
                        Content = param.Content,
                        CreateTime = DateTime.Now,
                        CreateBy = param.LoginID,
                        Status = false,
                        SerialNumber = XHPartyTransmission.GetSerialNumber()
                    };

                    hdID = FA.ID;
                    if (!XHPartyTransmission.InsertData(FA))
                    {
                        result.code = "0";
                        result.desc = "输入数据有误，请确认后重试。";

                        isFlag = false;
                    }
                    else
                    {
                        DataTable dta = XHPartyTransmission.GetSerialNumber(hdID);
                        if (dta.Rows.Count > 0)
                        { result.SerialNumber = dta.Rows[0]["SerialNumber"].ToString(); }
                        else
                        {
                            result.desc = "数据插入不成功，请确认。";
                        }
                    }

                }

                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "转移过程出现异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-ToXHPartyTransmission", SerializeHelper.JsonSerializer<ToXHPartyTransmissionInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<ToXHPartyTransmissionInParam, ToXHPartyTransmissionOutParam>();
            else
                SafeProcessRequest<ToXHPartyTransmissionInParam, ToXHPartyTransmissionOutParam>();
        }

    }
    #region 请求参数
    public class ToXHPartyTransmissionInParam
    {
        /// <summary>
        /// 组织ID	string	必须
        /// </summary>
        public string BranchID
        { get; set; }

        /// <summary>
        /// 登录ID	string	非必须
        /// </summary>
        public string LoginID
        { get; set; }

        /// <summary>
        /// 姓名	String  必填
        /// </summary>
        public string UserName
        { get; set; }

        /// <summary>
        /// 联系方式	string	 必填
        /// </summary>
        public string MobileNo
        { get; set; }

        /// <summary>
        /// 年龄	String	必填
        /// </summary>
        public string Age
        { get; set; }

        /// <summary>
        /// 性别	String	必填
        /// </summary>
        public string Gender
        { get; set; }

        /// <summary>
        /// 职务	String	必填
        /// </summary>
        public string Post
        { get; set; }

        /// <summary>
        /// 政治面貌	String	必填
        /// </summary>
        public string PoliticsLineament
        { get; set; }

        /// <summary>
        /// 工作单位	string        必填
        /// </summary>
        public string WorkUnit
        { get; set; }

        /// <summary>
        /// 联系地址	必填
        /// </summary>
        public string Address
        { get; set; }

        /// <summary>
        /// 邮编	必填
        /// </summary>
        public string Zip
        { get; set; }

        /// <summary>
        /// 党代表ID	String	必填
        /// </summary>
        public string PartyDeputys
        { get; set; }

        /// <summary>
        /// 党代表名称	String	
        /// </summary>
        public string PartyDeputyName
        { get; set; }

        /// <summary>
        /// 主题	String	必填
        /// </summary>
        public string Title
        { get; set; }

        /// <summary>
        /// 反映内容	String	必填
        /// </summary>
        public string Content
        { get; set; }


    }
    #endregion

    #region 结果参数
    public class ToXHPartyTransmissionOutParam
    {
        public ToXHPartyTransmissionOutParam()
        {
            code = "1";
            desc = "操作成功";
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 编码
        /// </summary>
        public string SerialNumber
        { get; set; }
    }
    #endregion
}